import Vue from 'vue'
import App from './App.vue'

Vue.config.productionTip = false

Vue.prototype.$dispatch = function (componentName,eventName) {
  const parent = this.$parent; 
  while(parent){
    let name =parent.$options.name;
    if(name==componentName){
      break;
    }else{
      parent =parent.$parent
    }
  }
  // console.log(parent)
  if(parent){
    if(eventName){
      return parent.$emit(eventName)
    }
    return parent
  }
  
}

Vue.prototype.$broadcast = function (componentName,eventName) {
    let children = this.$children ; 
    let arr =[];
    function findFormItem(children) {
      children.forEach(child=>{
       if(child.$options.name === componentName){
        arr.push(child);
        if(eventName){
          child.$emit(eventName)
        }
       }
       if (child.$children) {
        findFormItem(child.$children);
    }
      })
    }
    findFormItem(children)
    // console.log(arr)
    return arr 
}

new Vue({
  render: h => h(App),
}).$mount('#app')
